var backgroundAudioManager,lock;
function backgroundAudio(options){
  lock = false;
  console.log(777,backgroundAudioManager,lock);
   if (backgroundAudioManager) {
     lock = true;
    return backgroundAudioManager.onStop((res) => {
      //  createInnerAudioContext
       console.log('stop fun222');
      //  backgroundAudioManager = null;
       backgroundAudioManager = wx.createInnerAudioContext();
       backgroundAudioManager.autoplay = options.autoplay || true;
       backgroundAudioManager.startTime = options.startTime || 0;
       backgroundAudioManager.obeyMuteSwitch = options.obeyMuteSwitch || false;
       backgroundAudioManager.src = options.src;
     })

     backgroundAudioManager.stop();
   }else{

     //  createInnerAudioContext
    //  backgroundAudioManager = null;
     backgroundAudioManager = wx.createInnerAudioContext();
     backgroundAudioManager.autoplay = options.autoplay || true;
     backgroundAudioManager.startTime = options.startTime || 0;
     backgroundAudioManager.obeyMuteSwitch = options.obeyMuteSwitch || false;
     backgroundAudioManager.src = options.src;
   }


    //   // backgroundAudioManager = wx.getBackgroundAudioManager();
    //  backgroundAudioManager = wx.createInnerAudioContext();

    //   // backgroundAudioManager.title = options.title ||'破事儿';
    //   // backgroundAudioManager.epname = options.epname ||'破事儿';
    //   // backgroundAudioManager.singer = options.singer ||'破事儿';
    //   // backgroundAudioManager.coverImgUrl = options.coverImgUrl||'';

    //  /** createInnerAudioContext **/
    //  backgroundAudioManager.autoplay = options.autoplay || true;
    //  backgroundAudioManager.startTime = options.startTime || 0;
    //  backgroundAudioManager.obeyMuteSwitch = options.obeyMuteSwitch || false;
    //  backgroundAudioManager.src = options.src;
  
}

backgroundAudio.prototype.onPlay = function(callBack){
  if ("function" === typeof (callBack)) {
    
     backgroundAudioManager.onPlay((res)=>{
       console.log(' onPlay', backgroundAudioManager)
      callBack(res);
    })

  }
}

backgroundAudio.prototype.onEnded = function (callBack) {
  if ("function" === typeof (callBack)) {
     backgroundAudioManager.onEnded((res) => {
      callBack(res);
      backgroundAudioManager = null;
      lock = false;
    })
  }
}

backgroundAudio.prototype.onStop = function (callBack) {
  console.log('onStop fun111');
  if ("function" === typeof (callBack) && !lock) {
     backgroundAudioManager.onStop((res) => {
      console.log('onStop fun',res);
      backgroundAudioManager = null;
      callBack(res);

    })
  }
}

backgroundAudio.prototype.onError = function (callBack) {
  
  if ("function" === typeof (callBack)) {
     backgroundAudioManager.onError((res) => {
      callBack(res);
      backgroundAudioManager = null;
    })
  }
}

backgroundAudio.prototype.stop = function () {
 
  if (backgroundAudioManager)  backgroundAudioManager.stop();
}

backgroundAudio.prototype.onWaiting = function (callBack) {
  if ("function" === typeof (callBack)) {
     backgroundAudioManager.onWaiting((res) => {
      callBack(res);
    })
  }
}


module.exports = backgroundAudio


// module.exports = function backgroundAudio(options) {
//   const backgroundAudioManager = wx.getBackgroundAudioManager();
//   backgroundAudioManager.title = options.title||'';
//   backgroundAudioManager.epname = options.epname||'';
//   backgroundAudioManager.singer = options.singer||'';
//   backgroundAudioManager.coverImgUrl = options.coverImgUrl||'';
//   backgroundAudioManager.src = options.src ||'';
// }

// onTimeUpdate , onEnded , onWaiting , onError